<html>
<head>
<title>Using the System Class</title>
</head>
<body>
<table width=100%>
<tr>
<td align=left>
<a href="index.html"><img src=../../images/PreviousArrow.gif width=26 height=26 align=bottom border=0 alt="Previous | "></a><a
href="ostreams.html"><img src=../../images/NextArrow.gif width=26 height=26 align=bottom border=0 alt="Next | "></a><a
href="../../index.html"><img src=../../images/WayUpArrow.gif width=26 height=26 align=bottom border=0 alt="Trail Map | "></a><a
href="../index.html"><img src=../../images/toolHeader.gif width=26 height=26 align=bottom border=0 alt="The Java Development Environment | "></a>
<td>
<td align=right>
<a href="index.html"><strong><em>Using System Resources</em></strong></a>
</td>
</tr>
</table>
<p>
<hr size=4>

<h2>
    Using the System Class
</h2>
<p>
<blockquote>
 
Unlike most other classes, you don't instantiate the System
class to use it.**  All of System's variables and methods are
class variables and class methods; they are declared <code>static</code>.
For more information about class variables and methods and how they
differ from instance variables and methods, refer to
<a href="../../java/anatomy/static.html">Static vs. Instance</a>
<a href="../../java/anatomy/static.html"><img src="../../images/javaIcon.gif" height=20 width=20 border=0></a>
in the <em>The Anatomy of a Java Application</em>.
<p>
To use a class variable, you use it directly from the name of the class using Java's
dot ('.') notation. For example, to reference the System's class variable <code>out</code>,
you append the variable name (<code>out</code>) to the end of the class name
(<code>System</code>) separated by a period ('.') like this:
<blockquote>
<pre>
System.out
</pre>
</blockquote>
You call class methods in a similar fashion but you need to indicate to the compiler
that this is a method (not a variable). You do this with parentheses ( and ). Any
arguments to the method go between the two parentheses; if there are no arguments,
nothing appears between the parentheses. So, to reference the System's class method
<code>getProperty()</code>, you append the method name (<code>getProperty</code>) to
the end of the class name (<code>System</code>) separated by a period ('.') and you
append the parenthesis and arguments after that like this:
<blockquote>
<pre>
System.getProperty(<i>argument</i>);
</pre>
</blockquote>
This small Java program uses the System class (twice) to
retrieve the current user's name and display it.
<blockquote>
<pre>
class UserNameTest {
    public static void main(String args[]) {
	String name;
	name = <strong>System.getProperty("user.name");
	System.out.println(name);</strong>
    }
}
</pre>
</blockquote>
You'll notice that the program never instantiated
a System object; it just referenced the <code>getProperty()</code> method
and the <code>out</code> variable directly from the class.
<p>
System's <code>getProperty()</code> method used in the code sample
searches the properties database for the property called <b>user.name</b>.
<code>System.out</code> is a PrintStream that implements the standard
output stream. The <code>System.out.println()</code> method prints it argument
to the standard output stream. The next page of this lesson discusses System's
standard output stream.

<p>
** To be more precise, you can't instantiate the System class--it's a final
class and all of its constructors are private.

</blockquote>
<p>
<hr size=4>
<p>
<table width=100%>
<tr>
<td align=left>
<a href="index.html"><img src=../../images/PreviousArrow.gif width=26 height=26 align=top border=0 alt="Previous | "></a><a
href="ostreams.html"><img src=../../images/NextArrow.gif width=26 height=26 align=top border=0 alt="Next | "></a><a
href="../../index.html"><img src=../../images/WayUpArrow.gif width=26 height=26 align=top border=0 alt="Trail Map | "></a><a
href="../index.html"><img src=../../images/toolHeader.gif width=26 height=26 align=top border=0 alt="The Java Development Environment | "></a>
<td>
<td align=right>
<a href="index.html"><strong><em>Using System Resources</em></strong></a>
</td>
</tr>
</table>
</body>
</html>
